
gridWidth = $('.u_toolbar');

$(function(){

    _gridInit();

    $('li[data-opt="add"]').on('click', addCode);
    $('li[data-opt="edit"]').on('click', function(){
        var rowId = _grid.jqGrid('getGridParam', 'selrow');
        editCode(rowId);
    });
    $("li[data-opt='delete']").on('click', function(){
        var rowId = _grid.jqGrid('getGridParam', 'selrow');
        deleteCode(rowId);
    });
    $("li[data-opt='opt_search']").find(">i").on('click', function(){
        gridSearch.Content = {
            SearchText: $("#opt_search").find(">input").val()
        };
        search();
    });

    $('.modal-data').draggable();

});

var _grid = $('#jgTable'); 

function addCode(){
    $('#m_Code').val('');
    $('#m_Name').val('');
    $('.modal-overlay').removeClass('hidden');
    var _postData = Object.create(postData);
    _postData.Method = 'A';
    submit(_postData);
}
function editCode(rowId){
    if(rowId){  //判断是否选中有效行
        var rowData = _grid.jqGrid('getRowData', rowId);
        $('#m_Code').val(rowData.Code);
        $('#m_Name').val(rowData.Name);
        $('.modal-overlay').removeClass('hidden');
        var _postData = Object.create(postData);
        _postData.Method = 'U';
        submit(_postData);
    }else{
        myAlert('请选择您要编辑的参数');
    }
}
function deleteCode(rowId){
    if(rowId){  //判断是否选中有效行
        //var rowData = _grid.jqGrid('getRowData', rowId);
        var _postData = Object.create(postData);
        _postData.Method = 'D';
        console.log(_postData)
    }else{
        myAlert('请选择您要删除的参数');
    } 
}
function submit(_postData){
    $('#submitBtn').unbind('click').bind('click', function(){
        _postData.Content = {
            Code: $('#m_Code').val(),
            Name: $('#m_Name').val()
        }
        _postData.Method == 'A' && (_postData.Content.Code = 0);
        console.log(_postData)
        $.ajax({
            type: "post",
            url: mdmPath + "/MDM0008/MDM000801",
            data: _postData,
            dataType: "json",
            success: function (re) {
                if(re.Status == 200){
                    _grid.trigger('reloadGrid');
                    $('.modal-overlay').addClass('hidden');
                }else{
                    myAlert(re.Message);
                }
            }
        });
    });
}

function alarmFormatter(cellvalue, options, rowdata){
    var grid = $(this);
    if(JSON.stringify(rowdata)!=='{}'){
        cellvalue = '<a href="javascript:0" opt-data="'+grid[0].id+'_edit" title="编辑"><i class="fa fa-pencil"></i></a>'+
            '&nbsp;<a href="javascript:0" opt-data="'+grid[0].id+'_delete" title="删除"><i class="fa fa-trash-o"></i></a>';
    }else{
        cellvalue = '';
    }
    return cellvalue;
}
function _gridInit(){
    var _gridData = Object.create(gridData);
    _gridData.url = mdmPath + '/MDM0008/MDM000802';
    _gridData.colModel = [
        {label:'代码', name:'Code'},
        {label:'名称', name:'Name'},
        {label:'操作', name:'Options', width:80, align:'center', formatter: alarmFormatter}
    ];
    _gridData.sortname = 'Code';
    var _postData = {
        Method: 'Q',
        Content:{
            SearchText: ''
        }
    };
    for(i in postData){
        _postData[i] = postData[i];
    }
    //console.log(postData)
    _gridData.beforeRequest = function(){
        _gridData.grid.jqGrid("setGridParam", {
            postData : _postData,
        });
    };
    _gridData.loadComplete = function(xhr){
        xhr.Status == -999 && alert_QX($('body'), xhr.Message);
        $('a[opt-data="jgTable_edit"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            editCode(rowId);
        });
        $('a[opt-data="jgTable_delete"]').unbind('click').bind('click', function(){
            var rowId = $(this).parent().parent()[0].id;
            deleteCode(rowId);
        });
    };
    gridInit(_gridData);
    gridSearch = _postData;
}